<?php

class Helper_LoadFiles extends Zend_View_Helper_Abstract
{
public function load($type){
    
    switch($type){
        case('avatar'):
            $dir = "users/";
            $size = 60;
        break;
        
        case('photo'):
            $dir = "photos/";
            $size = 170;
        break;
    }

                $uploaddir  = '/public/upload/'.$dir;
                $name1 = NULL;
                $name1 = basename($_FILES['uploadfile']['name']);
                        if(!empty($name1)){
                                $file =  $uploaddir.$name1;
                                $file11 = explode('.', $name1);

                                $prev1 = $file11[0].'_min.'.$file11[1];

                                if  (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
                                        $this->resize($uploaddir.$name1, $size, $uploaddir.$prev1);
                                        
                                       switch($type){
                                           case('photo'):
                                              
                                              
                                              $_SESSION['photo']['index_img'] = $name1;
                                              $_SESSION['photo']['index_min'] = $prev1;
                                            break;

                                            case('avatar'):
                                              $_SESSION['avatar'] = $prev1;
                                            break;
                                       }

                                } 
                        }
   }
   public function resize($photo_src, $width, $name){
         $parametr = getimagesize($photo_src);
         list($width_orig, $height_orig) = getimagesize($photo_src);
         $ratio_orig = $width_orig/$height_orig;
         $new_width = $width;
         $new_height = $width / $ratio_orig;
        $newpic = imagecreatetruecolor($new_width, $new_height);
        switch ( $parametr[2] ) {
          case 1: $image = imagecreatefromgif($photo_src);
          break;
          case 2: $image = imagecreatefromjpeg($photo_src);
          break;
          case 3: $image = imagecreatefrompng($photo_src);
          break;
        }
        imagecopyresampled($newpic, $image, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
        imagejpeg($newpic, $name, 100);
        return true;
    }
}
?>
